package org.dromara.admin.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.CompanyBo;
import org.dromara.admin.domain.vo.CompanyVo;
import org.dromara.admin.service.ICompanyService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台端主体信息
 *
 * @author Lion Li
 * @date 2025-10-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/company")
public class AdminCompanyController extends BaseController {

    private final ICompanyService companyService;

    /**
     * 查询主体信息列表
     */
//    @SaCheckPermission("system:company:list")
    @GetMapping("/list")
    public TableDataInfo<CompanyVo> list(CompanyBo bo, PageQuery pageQuery) {
        return companyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出主体信息列表
     */
//    @SaCheckPermission("system:company:export")
    @Log(title = "主体信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CompanyBo bo, HttpServletResponse response) {
        List<CompanyVo> list = companyService.queryList(bo);
        ExcelUtil.exportExcel(list, "主体信息", CompanyVo.class, response);
    }

    /**
     * 获取主体信息详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:company:query")
    @GetMapping("/{id}")
    public R<CompanyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(companyService.queryById(id));
    }

    /**
     * 新增主体信息
     */
//    @SaCheckPermission("system:company:add")
    @Log(title = "主体信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CompanyBo bo) {
        return toAjax(companyService.insertByBo(bo));
    }

    /**
     * 修改主体信息
     */
//    @SaCheckPermission("system:company:edit")
    @Log(title = "主体信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CompanyBo bo) {
        return toAjax(companyService.updateByBo(bo));
    }

    /**
     * 删除主体信息
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:company:remove")
    @Log(title = "主体信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(companyService.deleteWithValidByIds(List.of(ids), true));
    }

    // TODO 后台管理公司主体信息状态：正常、冻结
}
